package fs;

import java.awt.*;
import java.io.*;
import java.util.*;
import java.text.*;

import language.*;
import misc.*;

public final class FileProc {
    public static String[] fileList() {
        File f = new File(Props.currentPath);
        File files[] = f.listFiles();
        String template = "FILENAME   EXT  PROPS  DATETIME                  SIZE";
        StringBuilder sb = new StringBuilder();
        ArrayList<String> ss = new ArrayList<>();
        long totalSize = 0;
        int fileCount = 0;
        ss.add(template);
        for (int i = 0; i < template.length(); i++) sb.append("-");
        ss.add(sb.toString());
        if (files != null) {
            for (File file: files) {
                String s = new String(template);
                String[] fn = file.getName().split("\\.");
                totalSize += file.length();
                double size = (double) file.length();
                String dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified()));
                if (fn.length < 2) fn = new String[] {fn[0], ""};
                while (fn[0].length() < 8) fn[0] += " ";
                while (fn[1].length() < 3) fn[1] += " ";
                s = s.replace("FILENAME", fn[0].toUpperCase());
                s = s.replace("EXT", fn[1].toUpperCase());
                if (file.isDirectory()) s = s.replace("PROPS", "<DIR>");
                else {
                    s = s.replace("PROPS", "<FIL>");
                    fileCount++;
                }
                s = s.replace("DATETIME           ", dt);
                int type;
                if (size >= 0 && size < 1024) {
                    type = 1;
                } else if (size >= 1024 && size < 1048576) {
                    type = 2;
                    size /= 1024;
                } else if (size >= 1048576 && size < 1073741824) {
                    type = 3;
                    size /= 1048576;
                } else {
                    type = 4;
                    size /= 1073741824;
                }
                size = (int) (size * 10) / 10.0;
                String ssize = new Double(size).toString();
                switch (type) {
                    case 1: ssize += "B"; break;
                    case 2: ssize += "KB"; break;
                    case 3: ssize += "MB"; break;
                    case 4: ssize += "GB"; break;
                }
                s = s.replace("SIZE", ssize);
                ss.add(s);
            }
        }
        ss.add("");
        ss.add("         " + fileCount + LangEnv.files + "         " + totalSize + "B");
        ss.add("");
        return ss.toArray(new String[] {});
    }
    public static boolean createFile(String fn) {
        boolean b = false;
        try {
            b = new File(Props.currentPath + File.separator + fn).createNewFile();
        } catch (Exception ex) {
            b = false;
        }
        return b;
    }
    public static String[] getContent(String fn) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(Props.currentPath + File.separator + fn)));
            char abab[] = new char[(int) new File(Props.currentPath + File.separator + fn).length()];
            reader.read(abab);
            String s = new String(abab);
            reader.close();
            return s.split("\n");
        } catch (Exception ex) {
            return new String[] {LangEnv.fileNotExist};
        }
    }
    public static boolean deleteFile(String fn) {
        boolean b = false;
        try {
            b = new File(Props.currentPath + File.separator + fn).delete();
        } catch (Exception ex) {
            b = false;
        }
        return b;
    }
}